from typing import List, Optional
from pydantic import BaseModel, Field, EmailStr
from .base import BaseSchema
from .role import RoleSchema
from .department import DepartmentSchema

class UserBase(BaseSchema):
    """用户基础模型"""
    username: str = Field(..., max_length=50, description="用户名")
    email: Optional[EmailStr] = Field(None, description="邮箱")
    phone: Optional[str] = Field(None, max_length=20, description="手机号")
    department_id: Optional[int] = Field(None, description="部门ID")
    is_active: Optional[bool] = Field(True, description="是否激活")

class UserCreate(UserBase):
    """创建用户模型"""
    password: str = Field(..., min_length=6, description="密码")
    role_ids: Optional[List[int]] = Field(None, description="角色ID列表")

class UserUpdate(UserBase):
    """更新用户模型"""
    password: Optional[str] = Field(None, min_length=6, description="密码")
    role_ids: Optional[List[int]] = Field(None, description="角色ID列表")

class UserSchema(UserBase):
    """用户响应模型"""
    status: str = Field("1", description="用户状态")  # 1: 启用, 0: 禁用
    is_active: bool = Field(True, description="是否激活")  # 为了兼容前端，保持此字段
    is_superuser: bool = Field(False, description="是否超级管理员")
    roles: List[RoleSchema] = Field([], description="角色列表")
    department: Optional[DepartmentSchema] = Field(None, description="部门信息")

class UserLogin(BaseModel):
    """用户登录模型"""
    username: str = Field(..., description="用户名")
    password: str = Field(..., description="密码")

class Token(BaseModel):
    """Token模型"""
    access_token: str = Field(..., description="访问令牌")
    token_type: str = Field("bearer", description="令牌类型")

class TokenData(BaseModel):
    """Token数据模型"""
    username: Optional[str] = Field(None, description="用户名")